package org.liaohailong.library.widget.common;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/**
 * Author: liaohailong
 * Date: 2022/8/28
 * Time: 9:48
 * Description: 点击反馈功能代理
 **/
class CommonClickDelegate {

    /**
     * 针对两种类型的drawable进行添加点击反馈的处理
     *
     * @param background ColorDrawable 在xml中直接设置颜色； GradientDrawable 在xml中引入shape通常处理圆角和边线
     * @return 封装好的点击反馈drawable
     */
    static Drawable getBackgroundDrawable(Drawable background) {
        if (background == null) return null;

        // 给没有点击效果的 background 添加一个点击反馈效果
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            // 5.0 以上使用水波纹效果
            Drawable pressedDrawable = getPressedDrawableApi21(background, 0XFFF2F2F2);
            if (pressedDrawable != null) {
                background = pressedDrawable;
            }
        } else {
            Drawable pressedDrawable = getPressedDrawableApi19(background, 0.5f);
            if (pressedDrawable != null) {
                background = getStateListDrawable(background, pressedDrawable);
            }
        }

        return background;
    }

    @Nullable
    static Drawable getPressedDrawableApi19(@Nullable Drawable drawable, float alphaFactor) {

        if (drawable == null) return null;
        // 在xml中，或者代码中设置 setBackgroundColor 的情况，会创建这个drawable
        if (drawable instanceof ColorDrawable) {
            ColorDrawable colorDrawable = (ColorDrawable) drawable;
            int color = colorDrawable.getColor();
            int alpha = Color.alpha(color);
            int red = Color.red(color);
            int green = Color.green(color);
            int blue = Color.blue(color);

            // 反馈颜色暂时只处理alpha
            alpha *= alphaFactor;
            int pressedColor = Color.argb(alpha, red, green, blue);
            return new ColorDrawable(pressedColor);
        }
        // 在xml中创建shape 带有形状，颜色，虚线，边线之类，会创建这个drawable
        else if (drawable instanceof GradientDrawable) {
            GradientDrawable gradientDrawable = (GradientDrawable) drawable;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                float[] cornerRadii = null;
                try {
                    cornerRadii = gradientDrawable.getCornerRadii(); // 四个角自定义
                } catch (Exception ex) {
                    // ignore
                }
                float cornerRadius = gradientDrawable.getCornerRadius(); // 统一圆角
                ColorStateList colorStateList = gradientDrawable.getColor();
                int color = colorStateList.getDefaultColor();


                int alpha = Color.alpha(color);
                int red = Color.red(color);
                int green = Color.green(color);
                int blue = Color.blue(color);

                alpha *= alphaFactor;
                int pressedColor = Color.argb(alpha, red, green, blue);

                GradientDrawable pressGradientDrawable = new GradientDrawable();
                pressGradientDrawable.setColor(pressedColor);
                if (cornerRadii != null) // setCornerRadii 与 setCornerRadius 互斥
                    pressGradientDrawable.setCornerRadii(cornerRadii);
                else
                    pressGradientDrawable.setCornerRadius(cornerRadius);
                return pressGradientDrawable;
            }
        }

        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    static Drawable getPressedDrawableApi21(@Nullable Drawable drawable, @ColorInt int rippleColor) {
        if (drawable == null) return null;

        boolean wrap = false;
        // 在xml中，或者代码中设置 setBackgroundColor 的情况，会创建这个drawable
        if (drawable instanceof ColorDrawable) {
            wrap = true;
        }
        // 在xml中创建shape 带有形状，颜色，虚线，边线之类，会创建这个drawable
        else if (drawable instanceof GradientDrawable) {
            wrap = true;
        }

        if (wrap) {
            ColorStateList rippleColorStateList = ColorStateList.valueOf(rippleColor);
            RippleDrawable rippleDrawable = new RippleDrawable(rippleColorStateList, drawable, null);
            StateListDrawable stateListDrawable = new StateListDrawable();
            stateListDrawable.addState(new int[0], rippleDrawable);
            return stateListDrawable;
        }

        return null;
    }


    @NonNull
    static StateListDrawable getStateListDrawable(@NonNull Drawable background,
                                                  @NonNull Drawable pressed) {
        StateListDrawable stateListDrawable = new StateListDrawable();

        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
        stateListDrawable.addState(new int[0], background);
        return stateListDrawable;
    }


    /**
     * 添加一个文字点击反馈
     *
     * @param color 默认字体颜色
     * @return 封装好的点击反馈 color
     */
    static ColorStateList getColorStateList(@ColorInt int color, float alphaFactor) {
        int[][] states = new int[2][];
        states[0] = new int[]{android.R.attr.state_pressed};
        states[1] = new int[]{};
        int[] colors = new int[2];

        int alpha = Color.alpha(color);
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);

        alpha *= alphaFactor;

        colors[0] = Color.argb(alpha, red, green, blue);
        colors[1] = color;

        return new ColorStateList(states, colors);
    }
}
